var util = require('../../utils/util');
var wxApi = require('../../utils/wxApi');
var wxRequest = require('../../utils/wxRequest');
import config from '../../utils/config';

import {$wuxToptips} from '../../components/wux';
import WxValidate from '../../assets/plugins/WxValidate';
// import { $wuxXnumber } from '../../components/wux';
import { $wuxDialog } from '../../components/wux';


var app = getApp();
var that,wxbjdays;

var _nowdate = new Date();
var strdate = util.formatTime(_nowdate);
var strtime = strdate.substr(11);
strdate = strdate.substr(0,10);

//获取应用实例
var app = getApp()
Page({
  data: {
    user: {},
    nowdate:_nowdate,
    form: {
      name: "", 
      issecret: 0, 
      pwd: '', 
      bdate: strdate, 
      edate: strdate, 
      ldate: strdate, 
      award: "",
      remark: "",
      bjdays: 1,  
      awardnum: 0, 
      time:strtime,
      key:""
    },
    issecret: [
      {
        name: '公开', 
        value: 0, 
        checked: true, 
      },
      {
        name: '隐私', 
        value: 1, 
      }
    ],
    tid:0,

    input: '',
    todos: [
      // {name:"xxxx",id:"1"}
      // ,{name:"yyyy",id:"2"}
    ],
    curindex:-1
    ,sheight:450
    ,sheight2:450*0.2
  },
  onLoad: function (options) {
    that = this;

    wx.setNavigationBarTitle( { title: "基础数据修改" });
    that.initValidate();
    that.loadData();

  },
  loadData: function () {
    util.showLoading();
    app.getUserInfo()
    .then(res => {
      that.setData({
        user:res
      });
    })
    .then(res=>{
      var url = config.url_get_sysset;
      var params = {
      }
      return wxRequest.postRequest(url, params)
    })
    .then(res => {
        that.setData({form:res});
    })
    .catch(res => {
      util.alert_catch(res.message);
    })
    .finally(function (res) {
        util.hideLoading();
    });
  },
  showToptips(error) {
    const hideToptips = $wuxToptips.show({
      timer: 3000,
      text: error.msg || '请填写正确的字段',
      success: () => console.log('toptips', error)
    })
  },
  submitForm(e) {
    const data = e.detail.value

    if (!this.WxValidate.checkForm(e)) {
      const error = this.WxValidate.errorList[0]
      this.showToptips(error)
      return false
    }
     
    util.showLoading();
    var url = config.url_edit_sysset;
 
    var params = data;
    wxRequest.postRequest(url, params)
    .then(res => {
        if(res==22){
            util.alert("kb获取最小800步");
        }
        else if(res ==21){
            util.alert("无权非法操作");
        }
        else if(res ==1){
            util.alert("提交成功",function(){
                wx.navigateBack({
                    delta: 1
                })
            });
        }
    })
    .catch(res => {
        console.log(res);
        util.alert_catch(res.message);
    })
    .finally(function (res) {
        util.hideLoading();
    });

  },
  initValidate() {
      this.WxValidate = new WxValidate(
        {
            kblimit: {
        
            },
                step_cal: {
              required: true, 
            },
                step_cake: {
              required: true, 
            },
      }
      , 
      {
            kblimit: {
              //required: "请输入获取Kb的基本数据", 
            },
              step_cal: {
                  required: "请输入多少步等于1卡路里", 
            },
              step_cake: {
                  required: "请输入多少步等于1蛋糕", 
            },
        
      }
    );
  },
  bindbdate: function(e) {
    this.setData({
      'form.bdate': e.detail.value
    })
  },
  bindedate: function(e) {
    this.setData({
      'form.edate': e.detail.value
    })
  },
  bindldate: function(e) {
    this.setData({
      'form.ldate': e.detail.value
    })
  },
  bindtime: function(e) {
    this.setData({
      'form.time': e.detail.value
    })
  },
  bindawardnum: function (e) {
    this.setData({
      'form.awardnum': e.detail.value
    })
  },
  bindissecret:function (e) {
    var value =  e.detail.value;
    const issecret = this.data.issecret;
    issecret.forEach(n => n.checked = n.value == value)
    this.setData({
      issecret: issecret, 
      'form.issecret': value, 
    })
  },


  inputChangeHandle: function (e) {
    this.setData({input: e.detail.value})
  }

})